package com.cards_game.simple_ddz.handler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cards_game.simple_ddz.bean.UserBean;
import com.cards_game.simple_ddz.common.AllPrimaryContainer;
import com.cards_game.simple_ddz.common.RoomBean;
import com.cards_game.simple_ddz.service.EnterGameServic;

/**
 * 进入游戏后一些操作，包括进入大厅，进入房间，快速开始，准备等
 * @author 郭国辉
 * 
 *     2017年6月22日下午4:23:00
 */
@Controller
public class EnterGameHandler {
	@Resource
	private EnterGameServic enterGameServic;
	/**
	 * 进入游戏大厅，获取当前所有房间状况
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="enterGameLobby.do", produces = "text/html;charset=UTF-8")
	public List<RoomBean> enterGameLobby(HttpServletRequest request, ModelMap modelMap){
		Collection<RoomBean> rbs=AllPrimaryContainer.getAllPrimaryContainer().getAllRoomMap().values();
		return new ArrayList<>(rbs);
	}
	/**
	 * 点击房间进入
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="enterGameRoom.do", produces = "text/html;charset=UTF-8")
	public boolean enterGameRoom(HttpServletRequest request, ModelMap modelMap){
		String userId=request.getParameter("userId");
		String roomId=request.getParameter("roomId");
		boolean boo=enterGameServic.enterGameRoom(Integer.parseInt(userId),roomId);
		return boo;
	}
	/**
	 * 快速开始游戏
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="quickGame.do", produces = "text/html;charset=UTF-8")
	public RoomBean quickGame(HttpServletRequest request, ModelMap modelMap){
		UserBean ub=(UserBean) request.getSession().getAttribute("userBean");
		int userId=ub.getId();
		RoomBean room=enterGameServic.quickGame(userId);
		if(null!=room){
			ub.setInRoom(true);
		}
		return room;
	}
	/**
	 * 玩家准备游戏
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="ready.do", produces = "text/html;charset=UTF-8")
	public void ready(HttpServletRequest request, ModelMap modelMap){
		String userId=request.getParameter("userId");
		String roomId=request.getParameter("roomId");
		enterGameServic.ready(roomId,userId);
	}
	/**
	 * 所有玩家准备游戏
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="allReady.do", produces = "text/html;charset=UTF-8")
	public boolean allReady(HttpServletRequest request, ModelMap modelMap){
		String roomId=request.getParameter("roomId");
		boolean boo=enterGameServic.allReady(Integer.parseInt(roomId));
		return boo;
	}
	
}
